package com.hdoit.kylin.web.document.controllers;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.alibaba.druid.sql.PagerUtils;
import com.hdoit.kylin.logical.document.model.ReportCataLog;
import com.hdoit.kylin.logical.document.model.ReportCataLogEmployee;
import com.hdoit.kylin.logical.document.service.IReportCataLogEmployeeService;
import com.hdoit.kylin.logical.document.service.IReportCataLogService;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.document.ReportCataLogEmployeeSearch;
import com.hdoit.kylin.logical.viewmodel.document.ReportCataLogSearch;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;

/**
 * @author      guoshuren
 * @email       
 * @createtime  2018年02月01日 20:51:07
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/document/reportCataLog/")
public class ReportCataLogController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/document/reportCataLog/";
    }

    private IReportCataLogService reportCataLogService;
    private IReportCataLogEmployeeService reportCataLogEmployeeService;
    private ITeacherStudentService teacherStudentService;
    
    public ITeacherStudentService getTeacherStudentService() {
		return teacherStudentService;
	}
    
    @Resource
	public void setTeacherStudentService(
			ITeacherStudentService teacherStudentService) {
		this.teacherStudentService = teacherStudentService;
	}
	public IReportCataLogService getReportCataLogService() {
        return reportCataLogService;
    }
    @Resource
    public void setReportCataLogService(IReportCataLogService reportCataLogService) {
        this.reportCataLogService = reportCataLogService;
    }

    public IReportCataLogEmployeeService getReportCataLogEmployeeService() {
		return reportCataLogEmployeeService;
	}
    
    @Resource
	public void setReportCataLogEmployeeService(
			IReportCataLogEmployeeService reportCataLogEmployeeService) {
		this.reportCataLogEmployeeService = reportCataLogEmployeeService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="reportCataLogDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private List<String> employeeIds = new ArrayList<String>();
    
    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ReportCataLogSearch search,String reportType,boolean ajaxPost){
    	employeeIds.clear();
    	employeeIds.add(getLoginModel().getLoginId());
        
        //查询导师文件列表
        if(CommonWebUtils.isTeacher(getLoginModel())){
        	TeacherStudentSearch teacherStudentSearch = new TeacherStudentSearch();
        	teacherStudentSearch.setTeacherId(getLoginModel().getLoginId());
        	List<TeacherStudent> students = teacherStudentService.queryList(teacherStudentSearch,null);
        	for(TeacherStudent student:students){
        		employeeIds.add(student.getStudentId());
        	}
        }
        System.err.println(employeeIds.size());
        //查询共享文件列表
        	List<ReportCataLogEmployee> reportCataLogEmployees = reportCataLogEmployeeService.queryListByEmployee(getLoginModel().getLoginId());
        	List<String> reportIds = new ArrayList<String>();
        	for(ReportCataLogEmployee temp:reportCataLogEmployees){
        		reportIds.add(temp.getReportCataLogId());
        	}
        	List<ReportCataLog> shareReportCataLogs = reportCataLogService.queryListByReportCataLogIds(reportIds);
        	
        	System.err.println(reportIds.size());

        search.setEmployeeIds(employeeIds);
        search.setReportType(reportType);
        //TO-DO
    	pagerModel.setTotalAmount(reportCataLogService.queryCount(search));
        
        List<ReportCataLog> reportCataLogs= reportCataLogService.queryList(search,pagerModel);
        reportCataLogs.addAll(shareReportCataLogs);
        
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("reportCataLogs", reportCataLogs);
        model.addAttribute("reportCataLogSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value= outputViewName, method={RequestMethod.GET,RequestMethod.POST})
    public String output(Model model, ReportCataLogSearch search,String reportType){
    	List<ReportCataLogEmployee> reportCataLogEmployees = reportCataLogEmployeeService.queryListByEmployee(getLoginModel().getLoginId());
    	List<String> reportIds = new ArrayList<String>();
    	for(ReportCataLogEmployee temp:reportCataLogEmployees){
    		reportIds.add(temp.getReportCataLogId());
    	}
    	List<ReportCataLog> shareReportCataLogs = reportCataLogService.queryListByReportCataLogIds(reportIds);
    	
    	search.setEmployeeIds(employeeIds);
    	search.setReportType(reportType);
    	System.err.println(reportType);
    	List<ReportCataLog> reportCataLogs = reportCataLogService.queryList(search,null);
    	reportCataLogs.addAll(shareReportCataLogs);
    	
    	model.addAttribute("reportCataLogs",reportCataLogs);
    	model.addAttribute("reportCataLogSearch", search);
    	
    	return view(outputViewName);
    }
    
    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String reportCataLogDetailed(Model model,String key){
        ReportCataLog reportCataLog=new ReportCataLog();
        if(!StringUtils.isEmpty(key)){
            reportCataLog=reportCataLogService.queryModel(key);
        }
        model.addAttribute("reportCataLog", reportCataLog);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ReportCataLog reportCataLog){
        if(StringUtils.isEmpty(reportCataLog.getId())){
            reportCataLog.setId(CommonUtils.getKeyId());
            reportCataLog.setModelStatus(ModelStatus.add);
        }else{
            reportCataLog.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(reportCataLogService.applyChange(reportCataLog)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("reportCataLog", reportCataLog);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(reportCataLogService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
